import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 三石
 * Date: 2022-10-03
 * Time: 21:47
 */
public class MyQueue {

        public int[] elem;
        public int usedSize;
        public int front;//队头下标
        public int rear;//队尾下标

       // public MyCircularQueue(int k) {
       //     this.elem = new int[k];
       // }
    public MyQueue(int k) {
        this.elem = new int[k];
    }

        //入队
        public boolean enQueue(int value) {
            if(isFull()) {
                //扩容 需要注意
                return false;
            }
            this.elem[rear] = value;
            //千万不可以加加 ：   rear++;
            rear = (rear+1) % elem.length;
            this.usedSize++;
            return true;
        }
        //出队
        public boolean deQueue() {
            if(isEmpty()) {
                return false;
            }
            front = (front+1)% elem.length;
            this.usedSize--;

            return true;
        }

        //获取队头元素
        public int Front() {
            if(isEmpty()) {
                return -1;
            }
            return elem[front];
        }

        //获取队尾元素
        public int Rear() {
            if(isEmpty()) {
                return -1;
            }
            int index = (rear == 0) ? elem.length-1 : rear-1;
            return elem[index];
        }

        public boolean isEmpty1() {
            return rear == front;
        }

        public boolean isFull2() {
            return (rear+1)% elem.length == front;
        }

        public boolean isEmpty() {
            return usedSize==0;
        }

        public boolean isFull() {
            return usedSize ==  elem.length;
        }


    /**
     * 用两个栈实现队列
     */
    //先new两个栈
    Stack<Integer> s1;
    Stack<Integer> s2;

    public MyQueue(){
        s1=new Stack<>();
        s2=new Stack<>();
    }
    public void push(int x){
        s1.push(x);
    }

    public int pop(){
        if (empty()){
            return -1;  //说明此时两个栈都为空
        }
        if (s2.empty()){ //s2为空，s1不为空
            while (!s1.empty()){
                s2.push(s1.pop()); //把s1所有元素都倒过来，放进s2
            }
        }
            //此时s2一定有元素
            return s2.pop();
    }

    public int peek() {
        if(empty()) {
            return -1;
        }
        if(s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }


    public boolean empty(){
        return s1.empty()&& s2.empty();
    }
}
